/**
 * 
 */
package com.pmsco.pms.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import com.pmsco.pms.business.PmsMenuBusiness;
import com.pmsco.pms.business.dto.PmsMenuRequestDTO;
import com.pmsco.pms.business.dto.PmsMenuReturnDTO;
import com.pmsco.pms.core.constant.PmsConstant;
import com.pmsco.pms.core.domain.PmsMenu;
import com.pmsco.pms.util.PmsUserSession;

/**
 * @author Long Tran
 *
 */
public class AbstractPmsController {
	private static final Logger logger = LoggerFactory.getLogger(AbstractPmsController.class);
	PmsMenuBusiness pmsMenuBusiness = new PmsMenuBusiness();
	
	@InitBinder 
	public void initBinder(WebDataBinder binder) { 
		SimpleDateFormat dateFormat = new SimpleDateFormat(PmsConstant.dateFormat); 
		dateFormat.setLenient(false); 
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false)); 
	} 
	
	protected void buildMenuList(HttpSession session) {
		logger.info("Begin build menu list");
		
		PmsUserSession pmsUserSession = (PmsUserSession)session.getAttribute(PmsConstant.loggedUserSession);
		StringBuffer ret = new StringBuffer();
		PmsMenuRequestDTO pmsMenuRequestDTO = new PmsMenuRequestDTO();
		
		Long roleId = new Long(0);
		if(pmsUserSession != null) {
			roleId = pmsUserSession.getPmsRole().getId();
			logger.debug("Load menu for user has role: " + pmsUserSession.getUserRole().getRole());
			pmsMenuRequestDTO.setPmsRole(pmsUserSession.getPmsRole());
		}
		
		PmsMenuReturnDTO pmsMenuReturnDTO = pmsMenuBusiness.getTopMenuList(pmsMenuRequestDTO);
		List<PmsMenu> menuList = pmsMenuReturnDTO.getMenulist();
		ret.append("<ul>");
		for(PmsMenu menu : menuList) {
			if(pmsMenuBusiness.isMenuEnableForRole(menu, roleId)) {
				if(menu.getChildren() != null && menu.getChildren().size() > 0) {
					ret.append(buildParentMenu(menu, roleId));
				} else {
					ret.append("<li><a href='" + menu.getUrl() + "'>" + menu.getName() + "</a></li>");
				}
			}
		}
		ret.append("</ul>");
		session.setAttribute(PmsConstant.menuList, ret.toString());
		logger.debug("Menu build: " + ret.toString());
		logger.info("End build menu list");
	}
	
	private String buildParentMenu(PmsMenu menu, Long roleId) {
		StringBuffer ret = new StringBuffer();
		ret.append("<li><a href='" + menu.getUrl() + "'>" + menu.getName() + "</a><ul>");
		for(PmsMenu subMenu : menu.getChildren()) {
			if(pmsMenuBusiness.isMenuEnableForRole(subMenu, roleId)) {
				if(subMenu.getChildren() != null && subMenu.getChildren().size() > 0) {
					ret.append(buildParentMenu(subMenu, roleId));
				} else {
					ret.append("<li><a href='" + subMenu.getUrl() + "'>" + subMenu.getName() + "</a></li>");
				}
			}
		}
		ret.append("</ul></li>");
		return ret.toString();
	}
}
